//
//  HYTabBarController.swift
//  weibo-01
//
//  Created by 源 何 on 16/9/22.
//  Copyright © 2016年 何源. All rights reserved.
//

import UIKit

class HYTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //将UITabBarController 的属性 tabBar替换成自定义的tabbar
        //只读属性不能够直接设置值 可以通过KVC间接的设置值
        let hmTab = HYMainTabBar()
        
        hmTab.composeClosure = { [weak self] in
            print("撰写按钮被点击了!",self)
            print("--------------------------分割线--------------------------")
        }
        
        self.setValue(hmTab, forKey: "tabBar")

        //添加子视图控制器
        addChillViewControllers()
    }

    //MARK: 添加子控制器
    private func addChillViewControllers() {
        addChileViewController(vc: HYHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChileViewController(vc: HYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChileViewController(vc: HYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChileViewController(vc: HYProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK: 封装方法
    private func addChileViewController(vc: UIViewController,title: String,imageName: String) {
        //设置导航栏标题
        vc.navigationItem.title = title
        //设置tabBar标题
        vc.tabBarItem.title = title
        //设置tabBar
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中状态照片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置选中状态文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //设置偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        //修改徽标的颜色
        vc.tabBarItem.badgeColor = UIColor.black
        //修改徽标数量
        vc.tabBarItem.badgeValue = nil
        
        let nav = HYBaseNavController(rootViewController: vc)
        //addChildViewController: 如果自定义 tabBar 将tabBar上的tabBarButton隐藏 自己添加自定义按钮,并且还使用该方法 就会出现自定义按钮无法点击,使用 setViewControllers: animated: 就可以解决该问题
        addChildViewController(nav)
    }


}
